import numpy as np
from function import *
from random import randint


SCREEN_WIDTH = 1200
SCREEN_HEIGHT = 600


class _Object:
    def __init__(self, screen, image_path, image_name):
        self.screen = screen
        self.image_path = image_path
        self.image_name = image_name
        self.state = self.get_image_frames(image_name)
        self.state_array = image_array(self.image_path + '\\' + self.image_name)
        self.cycle = gif_frames_number(self.image_path + '\\' + image_name)
        self.frames_number = 0
        self.coordinate = (0, 0)
        self.command = self.run      # 点击后需要执行的函数

    def get_image_frames(self, image_name: str | list | tuple):
        if isinstance(image_name, str):
            return gif_to_images(self.image_path + '\\' + image_name)
        elif isinstance(image_name, (list, tuple)):
            return [gif_to_images(self.image_path + '\\' + name) for name in image_name]

    def update(self):
        self.screen.blit(self.state[self.frames_number % self.cycle], self.coordinate)

    def onclick(self, mouse_pos):
        mouse_pos = (mouse_pos[0] - self.coordinate[0], mouse_pos[1] - self.coordinate[1])
        if 0 < mouse_pos[0] < self.state_array.shape[0] and 0 < mouse_pos[1] < self.state_array.shape[1]:
            if self.state_array[mouse_pos[1], mouse_pos[0], 3] > 0:
                self.command()

    def run(self):
        pass


class Sun(_Object):
    def __init__(self, screen):
        super().__init__(screen, 'images\\interface', 'Sun.gif')
        start_coordinate = (randint(0, SCREEN_WIDTH - 100), 0)
        self.trajectory = [(start_coordinate[0], start_coordinate[1] + distance)
                           for distance in range(0, SCREEN_HEIGHT - 80, 5)]
        self.over_time = randint(80, 120)
        self.over = False

    def run(self):
        self.over = True

    def update(self):
        self.coordinate = self.trajectory[self.frames_number if self.frames_number < len(self.trajectory) else -1]
        if self.frames_number > self.over_time:
            self.over = True
        else:
            self.screen.blit(self.state[self.frames_number % self.cycle], self.coordinate)
            self.frames_number += 1


class CountSun(ImageObject):
    def __init__(self, screen):
        super().__init__(screen, 'images\\interface', 'SunBack.png')
        self.sun = 0
        self.sun_font = pg.font.Font(None, 50)
        self.sun_font_color = 'black'

    def update(self):
        text = self.sun_font.render(f'{self.sun * 25}', True, self.sun_font_color)
        self.screen.blit(self.state, self.coordinate)
        self.screen.blit(text, (self.coordinate[0] + 100, self.coordinate[1] + 15))


class RandomSun:
    def __init__(self, screen, sun_list: list[Sun]):
        self.screen = screen
        self.sun_list = sun_list
        self.time = pg.time.get_ticks()
        self.production = randint(SUN_PRODUCTION[0], SUN_PRODUCTION[1]) * 100

    def update(self):
        if pg.time.get_ticks() - self.time > self.production:
            self.production = randint(SUN_PRODUCTION[0], SUN_PRODUCTION[1]) * 100
            self.sun_list.append(Sun(self.screen))
            self.time = pg.time.get_ticks()
        self.sun_list = [sun for sun in self.sun_list if not sun.over]
        for sun in self.sun_list:
            sun.update()

    def onclick(self, mouse_pos, count: CountSun):
        for sun in self.sun_list:
            sun.onclick(mouse_pos)
            if sun.over:
                count.sun += 1
            sun.update()


class BackCard(ImageObject):
    def __init__(self, screen):
        super().__init__(screen, 'images\\interface', 'BackCard.png')
        self.coordinate = (100, 0)

